﻿module App.Services {
    import YahooStockQuote = App.Models.IYahooStockQuote;

    export interface IStockHistoryService {
        getHistory(symbol: string, startDate: string, endDate: string);
        StockQuotes: any[];
    }

    export class StockHistoryService implements IStockHistoryService {
        private url = "api/StockHistory/Get?";
        static $inject = ['$http'];
        StockQuotes: any[];
        public getHistory(symbol: string, startDate: string, endDate: string) {
            var endpoint = this.url + "symbol=" + symbol +
                "&startDate=" + encodeURIComponent(new Date(startDate).toJSON()) +
                "&endDate=" + encodeURIComponent(new Date(endDate).toJSON());
            console.log(endpoint);
            return this.$http.get<YahooStockQuote[]>(endpoint)
                .success(data => {
                    console.log(data);
                    while (this.StockQuotes.length > 0) {
                        this.StockQuotes.pop(); //remove all elements without declaring new array
                    }
                    for (var i = 0; i < data.length; i++) {
                        data[i].Date = new Date(data[i].Date.toString());
                        this.StockQuotes.push(data[i]); //push all new elements
                    }
                })
                .error(() => { console.log("There was an error!!!"); });
        }

        constructor(private $http: ng.IHttpService) {
            this.StockQuotes = [];
            return this;
        }
        ////Need to add guard clauses here for input
        //public getHistory(symbol: string, startDate: string, endDate: string) {
        //    this.$http.get<YahooStockQuote>(this.url + "symbol=" + symbol + "&startDate=" + startDate + "&endDate=" + endDate)
        //        .success(data => {
        //            console.log(data);
        //        });
        //}
    }
    angular
        .module("app")
        .service("StockHistoryService", ["$http", StockHistoryService]);
}